package com.csw.bootstrapjqgrid.controller;

import com.csw.bootstrapjqgrid.entity.Dept;
import com.csw.bootstrapjqgrid.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @author 1
 */
@RestController
@RequestMapping("d")
public class DeptController {
    @Autowired
    private DeptService deptService;

    @RequestMapping("qa")
    public List<Dept> qa() {
        List<Dept> depts = deptService.qa();
        depts.forEach(dept -> System.out.println("qa/dept/" + dept));
        return depts;
    }

    @RequestMapping("qa2")
    public void qa2(HttpServletResponse response) throws IOException {
        List<Dept> depts = deptService.qa();
        StringBuilder sb = new StringBuilder();
        sb.append("<select>");
        // 构建select代码
        depts.forEach(dept -> sb.append("<option value='").append(dept.getId()).append("'>").append(dept.getName())
                .append("</option>"));
        sb.append("</select>");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().print(sb.toString());
    }

    @RequestMapping("add")
    public String add(String deptname) {
        System.out.println("deptname/" + deptname);
        deptService.add(new Dept(UUID.randomUUID().toString(), deptname));
        return "redirect:/home.jsp";
    }

}
